﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LX.NHibernate.Utils
{
    public class JsonUtils
    {
        /// <summary>
        /// 返回处理过时间的json字符
        /// </summary>
        /// <param name="data"></param>
        /// <param name="DateTimeFormat">默认的时间格式</param>
        /// <returns></returns>
        public static string SerializeToJson(object data, string DateTimeFormats = "yyyy-MM-dd HH:mm:ss")
        {
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = DateTimeFormats };

            return JsonConvert.SerializeObject(data, Formatting.Indented, timeConverter);
        }

        /// <summary>
        /// 把json字符串转成对象
        /// </summary>
        /// <typeparam name="T">对象</typeparam>
        /// <param name="data">json字符串</param> 
        public static T DeserializeToModel<T>(string data)
        {
            return JsonConvert.DeserializeObject<T>(data);
        }
    }
}
